package com.mes.check.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mes.check.manager.iface.IDefectJudgeManager;
import com.mes.check.dao.iface.IDefectJudgeDao;
import com.mes.check.domain.DefectJudge;
import com.mes.common.domain.BasicModel;
import com.mes.common.manager.BasicManager;
import com.mes.common.util.SysUtil;
import com.mes.task.domain.OperationTask;
import com.mes.task.domain.WorkRecord;
import com.mes.task.manager.iface.IOperationTaskManager;

@Component
public class DefectJudgeManager extends BasicManager<DefectJudge> implements IDefectJudgeManager {
	private final static Log log = LogFactory.getLog(DefectJudgeManager.class);

	@Autowired
	private IDefectJudgeDao defectJudgeDao;
	@Autowired
	private IOperationTaskManager operationTaskManager;

	public void ChangeJudgeState(String billUid) throws Exception {
		log.info("不合格品审理单是：" + billUid);
		try {
			DefectJudge defectJudge = (DefectJudge) this.getById(billUid);
			int state = defectJudge.getJudgeBillState();
			if (state == DefectJudge.JUDGE_BILLSTATE_CREATED) {
				// 将不合格品审理单的状态从待发起变成已发起；
				defectJudge.setJudgeBillState(DefectJudge.JUDGE_BILLSTATE_UNCLOSED);
				defectJudge.setCreateTime(new Date());
				this.update(defectJudge);

				log.error("不合格品审理单" + billUid + "的状态从待发起变成已发起");
			} else if (state == DefectJudge.JUDGE_BILLSTATE_UNCLOSED) {
				// 将不合格品审理单的状态从已发起变成已结案；
				defectJudge.setJudgeBillState(DefectJudge.JUDGE_BILLSTATE_CLOSED);
				defectJudge.setJudgeTime(new Date());
				this.update(defectJudge);
				log.error("不合格品审理单" + billUid + "的状态从已发起变成已结案");

				String taskuid = defectJudge.getTaskUid();
				OperationTask operationTask = operationTaskManager.getById(taskuid);

				if (operationTask != null && operationTask.getTaskState() < 209) { // 如果审理单对应的工序任务没有完成，则反馈完成进度
					try {
						List workRecList = new ArrayList(10);
						// 对反馈信息进行初始化；
						WorkRecord workRec = new WorkRecord();
						workRec.setTaskUid(taskuid);
						String sessionEmployeeId = SysUtil.getSessionEmployeeUid();
						workRec.setWorkerUid(sessionEmployeeId);
						// 获取更改的当前时间；
						java.util.Date now = new java.util.Date();
						java.sql.Timestamp time = new java.sql.Timestamp(now.getTime());
						workRec.setCreateTime(time);
						// 设置加工日期
						Date workDate = now;
						java.sql.Timestamp date = new java.sql.Timestamp(workDate.getTime());
						workRec.setWorkDate(date);
						workRec.setCompleteQty(operationTask.getPlanQty() - operationTask.getCompleteQty());
						workRec.setCreatorUid(sessionEmployeeId);
						// workType＝1代表正常的加工记录；
						workRec.setWorkType(1);
						workRec.setObjectState(BasicModel.OBJECTSTATE_TOBEADDED); // 新增一条记录
						workRecList.add(workRec);

						operationTaskManager.feedbackTask(workRecList);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

}
